package com.teamone.etl.datadictionary.service.impl;

import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teamone.etl.datadictionary.dao.DatadictionaryDao;
import com.teamone.etl.datadictionary.dto.UpdateDto;
import com.teamone.etl.datadictionary.entity.Dictionary;
import com.teamone.etl.datadictionary.service.DatadictionaryService;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.UUID;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * @Description
 * @Author ZhaoPeiJie
 * @Date 2021/1/12 14:49
 * @Version 1.0
 */
@Service("DatadictionaryService")
@Transactional(rollbackFor = Exception.class)
public class DatadictionaryServiceImpl extends ServiceImpl<DatadictionaryDao,Dictionary> implements DatadictionaryService{

    @Resource
    private DatadictionaryDao datadictionaryDao;

    @Override
    public List<Dictionary> findAll() {
        return datadictionaryDao.findAll();
    }

    @Override
    public List<Dictionary> findByDictionaryName(Integer coding, String dictionaryName, Long current, Long limit) {
        return datadictionaryDao.findByDictionaryName(coding,dictionaryName,current,limit);
    }

    @Override
    public int addParamDictionary(Dictionary dictionary) {
        if(dictionary.getCoding()!=null
                && !StringUtils.isEmpty(dictionary.getDictionaryName())
                && !StringUtils.isEmpty(dictionary.getParentId())
                && dictionary.getIsSystem()!=null
                && dictionary.getStatus()!=null
                && dictionary.getDictionaryOrderby()!=null
                && !StringUtils.isEmpty(dictionary.getRemarks())){
            LocalDateTime localDateTime = LocalDateTime.now();
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String formatdate = localDateTime.format(dateTimeFormatter);
            dictionary.setCreateTime(formatdate);
            dictionary.setUpdateTime(formatdate);
            String id = UUID.randomUUID().toString();
            dictionary.setId(id);
            dictionary.setIsDelete(0);
            datadictionaryDao.addParamDictionary(dictionary);
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public List<String> findByParentIdString() {
        return datadictionaryDao.findByParentIdString();
    }

    @Override
    public void deleteDictionary(String id) {
        datadictionaryDao.deleteDictionary(id);
    }

    @Override
    public Dictionary findById(String id) {
        return datadictionaryDao.findById(id);
    }

    @Override
    public int updateDictionary(UpdateDto updateDto) {
        if(!StringUtils.isEmpty(updateDto.getId())
        && !StringUtils.isEmpty(updateDto.getRemarks())
                && !StringUtils.isEmpty(updateDto.getDictionaryName())
        && updateDto.getCoding()!=null
        && updateDto.getIsSystem()!=null
        && updateDto.getStatus()!=null
        && updateDto.getDictionaryOrderby()!=null){
            LocalDateTime localDateTime = LocalDateTime.now();
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String formatdate = localDateTime.format(dateTimeFormatter);
            updateDto.setUpdateTime(formatdate);
            datadictionaryDao.updateDictionary(updateDto);
            return 1;
        }else {
            return 0;
        }
    }
}
